<?php
/**
 * Created by PhpStorm.
 * User: Inhere
 * Date: 2015/2/6
 * Use : 一个表的架构信息,包含了各个字段列的信息(class Column)
 * File: Schema.php
 */

namespace ulue\core\db\base\schema;

use ulue\core\abstracts\AbstractCore;

class Schema extends AbstractCore
{

    /**
     * Property columns.
     * @var  Column[]
     */
    public $columns = [];

    // public $defaultValues = [];

    /**
     * Property indexes.
     * 一张表可以有多个主键索引
     *  e.g.
     * create table student(stu_id int, coures_id int, name varchar,
     *   primary key(stu_id,coures_id)
     *   );
     *       $define = array(
     *           'type' => 'KEY',
     *           'name' => null,
     *           'columns' => array(),
     *           'comment' => ''
     *       );
     * @var  array
     */
    public $indexes = [];


    /**
     * 一张表可以有多个主键索引
     *  e.g.
     * create table student(stu_id int, cour_id int, name varchar,
     *   primary key(stu_id,cour_id)
     *   );
     * Property indexes.
     * @var  string[]
     * Property prikey, primaryKeys.
     */
    public $priKey = [];

    /**
     * Property foreKey, foreignKeys.
     * @var  array
     */
    public $foreKey = [];

    /**
     * Property autoIncrement.
     * @var  int
     */
    public $autoIncrement = null;

    /**
     * Property engine.
     * @var  string
     */
    public $engine = null;

    /**
     * Property defaultCharset.
     * @var  string
     */
    public $defaultCharset = 'utf8';

    /**
     * Property collation.
     * @var  string
     */
    public $collation = 'utf8_general_ci';

    /**
     * Property comment.
     * @var  string
     */
    public $comment;

    /**
     * @param array   $columns
     * @param array   $indexes
     * @param integer $autoIncrement
     * @param string  $engine
     * @param string  $defaultCharset
     */
    public function __construct(array $columns = [],array $indexes = [], $autoIncrement = null, $engine = 'InnoDB', $defaultCharset = 'utf8')
    {
        $this->indexes        = $indexes;
        $this->engine         = $engine;
        $this->defaultCharset = $defaultCharset;
        $this->columns        = $columns;
        $this->autoIncrement  = $autoIncrement;
    }

    /**
     * setColumn
     * @param Column $column
     * @return  static
     */
    public function setColumn(Column $column)
    {
        $this->columns[$column->name] = $column;

        return $this;
    }

    /**
     * getColumn
     * @param string $name
     * @return  Column
     */
    public function getColumn($name)
    {
        if (empty($this->columns[$name]))
        {
            return null;
        }

        return $this->columns[$name];
    }

    /**
     * @return array
     */
    public function getColumnNames()
    {
        return array_keys($this->columns);
    }


}// end class Schema